﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MFToolkit.Net.XBee;

namespace XBeeDiag
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void TestButtonClick(object sender, RoutedEventArgs e)
        {
            var results = new StringBuilder();

            var serialPorts = SerialPort.GetPortNames();

            results.AppendFormat("Ports: {0}", string.Join(", ", serialPorts));
            results.AppendLine();
            results.AppendLine();

            foreach (var port in serialPorts)
            {
                results.AppendFormat("{0}: ", port);
                using (var xbee = new XBee(port))
                {
                    try
                    {
                        xbee.Open();
                        results.Append(xbee.Ping() ? "XBee found" : "No XBee found");
                    }
                    catch
                    {
                        results.Append("Exception!");
                    }
                    results.AppendLine();
                }
            }

            resultsBox.Text = results.ToString();
        }
    }
}
